package br.com.felix.dbm.ui.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;

import br.com.felix.dbm.controller.ColumnController;
import br.com.felix.dbm.database.DataType;
import br.com.felix.dbm.sql.DatabaseTypesFactory;
import br.com.felix.dbm.ui.Style;

public class DataTypesComboBox extends JComboBox{
	
	private static final long serialVersionUID = -1368844197330957427L;
	private ColumnController columnController;

	public DataTypesComboBox(ColumnController columnController){
		setFont(Style.getTextFont());
		setPreferredSize(Style.getComboBoxDimension());
		this.columnController = columnController;
		List<DataType> dbTypes = DatabaseTypesFactory.getDatabaseTypes().getDatabaseTypes();
		for(DataType dt: dbTypes){
			addItem(dt);
		}
		addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DataType newDataType = getSelectedDataType();
				if(newDataType!=null && !newDataType.equals(DataTypesComboBox.this.columnController.getModel().getType())){
					DataTypesComboBox.this.columnController.setType(newDataType);
					DataTypesComboBox.this.columnController.updateView();
				}
			}
		});
	}
	
	
	public DataType getSelectedDataType(){
		return (DataType)getSelectedItem();
	}

}
